package com.cskaoyan.service.vx;

import com.cskaoyan.bean.bo.vxBo.WXAftersaleSubmitBO;
import com.cskaoyan.bean.po.wxPo.*;
import com.cskaoyan.bean.vo.vxVo.WXAftersaleBean;
import com.cskaoyan.bean.vo.vxVo.WXAftersaleDetailVO;
import com.cskaoyan.bean.vo.vxVo.WXAftersaleOrderGoodsBean;
import com.cskaoyan.mapper.transfer.MarketAftersale2BeanVOMapper;
import com.cskaoyan.mapper.transfer.MarketAftersaleOrderGoods2BeanVOMapper;
import com.cskaoyan.mapper.wxMapper.MarketAftersaleMapper;
import com.cskaoyan.mapper.wxMapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.wxMapper.MarketOrderMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author by WANGJun
 * @date 2022/6/9 17:32
 */
@Service
public class WXAftersaleServiceImpl implements WXAftersaleService {

    @Autowired
    MarketAftersaleMapper marketAftersaleMapper;
    @Autowired
    MarketOrderMapper marketOrderMapper;
    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;

    @Override
    public WXAftersaleDetailVO detail(Integer orderId) {
        // WXAftersaleBean aftersale;
        MarketAftersaleExample marketAftersaleExample = new MarketAftersaleExample();
        MarketAftersaleExample.Criteria aftersaleExampleCriteria = marketAftersaleExample.createCriteria();
        aftersaleExampleCriteria.andOrderIdEqualTo(orderId);
        MarketAftersale marketAftersale = marketAftersaleMapper.selectByExample(marketAftersaleExample).get(0);
        WXAftersaleBean wxAftersaleBean = MarketAftersale2BeanVOMapper.INSTANCE.po2Vo(marketAftersale);
        // MarketOrder order;
        MarketOrderExample orderExample = new MarketOrderExample();
        MarketOrderExample.Criteria orderExampleCriteria = orderExample.createCriteria();
        orderExampleCriteria.andIdEqualTo(orderId);
        MarketOrder marketOrder = marketOrderMapper.selectByExample(orderExample).get(0);
        // List<WXAftersaleOrderGoodsBean> orderGoods;
        MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
        MarketOrderGoodsExample.Criteria orderGoodsExampleCriteria = orderGoodsExample.createCriteria();
        orderGoodsExampleCriteria.andOrderIdEqualTo(orderId);
        List<MarketOrderGoods> marketOrderGoods = marketOrderGoodsMapper.selectByExample(orderGoodsExample);
        List<WXAftersaleOrderGoodsBean> wxAftersaleOrderGoodsBeans = MarketAftersaleOrderGoods2BeanVOMapper.INSTANCE.po2Vo(marketOrderGoods);
        //构建WXAftersaleDetailVO
        WXAftersaleDetailVO wxAftersaleDetailVO = new WXAftersaleDetailVO();
        wxAftersaleDetailVO.setAftersale(wxAftersaleBean);
        wxAftersaleDetailVO.setOrder(marketOrder);
        wxAftersaleDetailVO.setOrderGoods(wxAftersaleOrderGoodsBeans);
        return wxAftersaleDetailVO;
    }

    @Override
    public void submit(WXAftersaleSubmitBO wxAftersaleSubmitBO) {
        //aftersale table
        MarketAftersale aftersale = new MarketAftersale();
        aftersale.setAmount(BigDecimal.valueOf(wxAftersaleSubmitBO.getAmount()));
        aftersale.setOrderId(Integer.parseInt(wxAftersaleSubmitBO.getOrderId()));
        aftersale.setPictures(wxAftersaleSubmitBO.toString());
        aftersale.setReason(wxAftersaleSubmitBO.getReason());
        aftersale.setType((short)(int) wxAftersaleSubmitBO.getType());
        aftersale.setComment(wxAftersaleSubmitBO.getTypeDesc());
        aftersale.setStatus((short)1);
        aftersale.setAddTime(new Date());
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        aftersale.setUserId(marketUser.getId());
        aftersale.setDeleted(false);
        marketAftersaleMapper.insertSelective(aftersale);
        //market_order table
        marketOrderMapper.updateOrderStatus(Integer.parseInt(wxAftersaleSubmitBO.getOrderId()),202);
    }
}
